<template>
  <div class="left-nav-container">
    <ul class="left-nav flex-direction-column">
      <p>导航目录</p>
      <li
        v-for="(item, index) in navList"
        :key="item._id"
        @click="setActive(index)"
        :class="{ active: index === activeId || (index === 0 && activeId === '') }"
      >
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'leftNavList',
  data() {
    return {
      activeId: ''
    }
  },
  props: {
    navList: {
      type: Array,
      default: () => []
    },
    rightIndex: {
      type: Number,
      default: 0
    }
  },
  watch: {
    rightIndex(newVal) {
      this.activeId = newVal
    }
  },
  methods: {
    // 设置当前激活的菜单
    setActive(index) {
      this.activeId = index
      // 触发父组件的 activeItem 事件
      this.$emit('activeItem', index)
    }
  }
}
</script>

<style lang="scss" scoped>
.left-nav {
  position: sticky;
  top: 40px;
  width: 180px;
  height: 540px;
  padding: 20px;
  overflow-y: hidden;
  overflow-x: hidden;
  background-color: #fff;
  border-radius: 20px;
  scrollbar-gutter: stable; // 确保滚动条出现是不会导致页面抖动
  &:hover {
    overflow-y: scroll;
  }
  p {
    color: #232323;
    font-weight: bold;
    font-size: 16px;
    text-align: center;
    margin-bottom: 20px;
  }
  li {
    height: 30px;
    padding: 10px 0;
    margin-top: 5px;
    line-height: 30px;
    text-align: center;
    cursor: pointer;
    border-radius: 8px;
    font-size: 14px;
    &:hover {
      background-color: rgba(255, 192, 203, 0.3);
    }
    &.active {
      background-color: pink;
    }
  }
}
</style>
